/*
 * @Author: hyq
 * @LastEditors: hyq
 * @Description: date
 * @Date: 2023-12-05 11:00:52
 */
enum FormatType {
  'YM',
  'YMD',
  'YMD_HMS'
}

const totalHour = 60 * 60 * 1000
const totalDay = 24 * totalHour
const totalWeek = 7 * totalDay

export function calculateInterVal(timestamp: number): string | number {
  const interval = Date.now() - timestamp
  if (interval < 0) return interval
  if (interval < totalHour) {
    return '近一小时内'
  } else if (interval < totalDay) {
    return Math.ceil(interval / totalHour) + '小时前'
  } else if (interval < totalWeek) {
    return Math.ceil(interval / totalDay) + '天前'
  } else {
    return new Date(timestamp).toLocaleString()
  }
}

export function formatDate(t: number | string, type?: number) {
  if (!t) return ''
  const date = new Date(t)
  const y = date.getFullYear()
  const mon = ('0' + (date.getMonth() + 1)).slice(-2)
  const days = ('0' + date.getDate()).slice(-2)
  const hours = ('0' + date.getHours()).slice(-2)
  const min = ('0' + date.getMinutes()).slice(-2)
  const second = ('0' + date.getSeconds()).slice(-2)
  if (type === FormatType.YMD) {
    return `${y}-${mon}-${days}`
  } else if (type === FormatType.YM) {
    return `${y}-${mon}`
  } else {
    return `${y}-${mon}-${days} ${hours}:${min}:${second}`
  }
}

//获取当天最后时间
export function formatDateToday() {
  const date = new Date()
  const y = date.getFullYear()
  const mon = ('0' + (date.getMonth() + 1)).slice(-2)
  const days = ('0' + date.getDate()).slice(-2)
  const hours = '23'
  const min = '59'
  const second = '59'
  return `${y}-${mon}-${days} ${hours}:${min}:${second}`
}

//获取传入时间的当天最后时间戳
export function getLastTimeStampChoose(t: number | string) {
  const date = new Date(t)
  const y = date.getFullYear()
  const mon = ('0' + (date.getMonth() + 1)).slice(-2)
  const days = ('0' + date.getDate()).slice(-2)
  const hours = '23'
  const min = '59'
  const second = '59'
  const newtime = `${y}-${mon}-${days} ${hours}:${min}:${second}`
  const newdate = +new Date(newtime)
  return newdate
}

export function getDayRemainSeconds() {
  const now = new Date()
  return (
    24 * 60 * 60 * 1000 -
    (now.getHours() * 60 * 60 * 1000 +
      now.getMinutes() * 60 * 1000 +
      now.getSeconds() * 1000)
  )
}
export function getDiffDay(date) {
  const current = new Date().getTime()
  const dataVal = new Date(date.replace(/-/g, '/')).getTime()
  const diffDate = Math.abs(current - dataVal)
  return Math.floor(diffDate / (1000 * 3600))
}
